package mj.scott.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mj.scott.common.Result;
import mj.scott.pojo.dto.DishDTO;
import mj.scott.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    private IDishService dishService;

    @PostMapping
    @ApiOperation("新增菜品接口")
    public Result<String> addDish(@RequestBody DishDTO dishDTO) {
        dishService.saveWithFlavor(dishDTO);
        return Result.success("新增菜品成功");
    }

    @GetMapping("/page")
    @ApiOperation("分页查询菜品接口")
    public Result<Page<DishDTO>> getDishPage(@RequestParam Integer page,
                                             @RequestParam Integer pageSize,
                                             @RequestParam(required = false) String name) {
        Page<DishDTO> dishDtoPage = dishService.getPage(page, pageSize, name);
        return Result.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据 id 查询菜品接口")
    public Result<DishDTO> getDishById(@PathVariable Long id) {
        DishDTO dishDTO = dishService.getByIdWithFlavor(id);
        return Result.success(dishDTO);
    }

    @PutMapping
    @ApiOperation("修改菜品接口")
    public Result<String> updateDish(@RequestBody DishDTO dishDTO) {
        dishService.updateWithFlavor(dishDTO);
        return Result.success("修改菜品成功");
    }

    @DeleteMapping
    @ApiOperation("删除菜品接口")
    public Result<String> deleteDish(@RequestParam List<Long> ids) {
        dishService.removeWithFlavor(ids);
        return Result.success("删除菜品成功");
    }

    @PostMapping("/status/{status}")
    @ApiOperation("批量启售/停售菜品接口")
    public Result<String> updateStatus(@PathVariable Integer status,
                                       @RequestParam List<Long> ids) {
        dishService.updateStatusBatch(status, ids);
        return Result.success("修改状态成功");
    }

    @GetMapping("/byCategory")
    public Result<Page<DishDTO>> getByCategory(
            @RequestParam Integer page,
            @RequestParam Integer pageSize,
            @RequestParam Long categoryId) {
        return Result.success(dishService.getByCategoryId(page, pageSize, categoryId));
    }
}